// 3. This do-file contains code for creating the appendix figures (Figures A1-A7 and B1) and tables (Table A1) for the manuscript. 

**** Set globals 
set more off
clear all

global dir_data "[FILL IN]" // FILL IN WITH FOLDER PATH FOR MAIN FILES
global temp "`c(tmpdir)'"

*********************
******Figure A1******
*********************
import excel using "${dir_data}/Aggregate_Benefit_Dollars.xlsx", ///
	sheet("Benefit_Dollars_Calendar_YR") firstrow clear case(lower)
	
**** Rename 
rename *calendar *
rename afdcandtanf tanf
rename medicaid med
rename workerscompensation wc
rename socialsecuritydisabilityinsur di
rename unemploymentinsurance ui
rename veteransdisabilitycompensatio vadc

**** Convert to billions
foreach var of varlist snap-vadc {
	replace `var' = `var'/1000000000
}

**** Adjust for inflation (using PCE)
preserve

** Import and clean raw data
import delimited "${dir_pce}/PCEPI.csv", clear

* Extract year/month
gen year = substr(date,1,4)
gen month = substr(date,6,2)
destring year month, replace
		
* Rename PCE variable
rename pcepi pce

* Collapse to annual
collapse (mean) pce, by(year)

* Save
compress
save "${temp}/pce", replace
		
restore
		
** Merge with PCE 
merge m:1 year using "${temp}/pce"
drop if _merge==2
drop _merge
		
** Generate 2019 multiplier
sum pce if year==2019
global pce_base = r(mean)
		
gen pce_multiplier = ${pce_base} * 1/pce

** Adjust dollars to 2019 dollars
foreach var of varlist snap-vadc {
	qui replace `var' = `var' * pce_multiplier
}

**** Calculate sum of seven non-Medicaid programs
egen nonmed_progs = rowtotal(snap wic tanf ssi wc di ui)

**** Figure
twoway (connected di year, lcolor(lavender) mcolor(lavender) msymbol(circle) yaxis(1)) ///
	(connected snap year, lcolor(navy) mcolor(navy) msymbol(diamond) yaxis(1)) ///
	(connected ssi year, lcolor(dkorange) mcolor(dkorange) msymbol(triangle_hollow) yaxis(1)) ///		
	(connected wc year, lcolor(maroon) mcolor(maroon) msymbol(triangle) yaxis(1)) ///
	(connected ui year, lcolor(forest_green) mcolor(forest_green) msymbol(square) yaxis(1)) ///	
	(connected tanf year, lcolor(khaki) mcolor(khaki) msymbol(circle_hollow) yaxis(1)) ///
	(connected wic year, lcolor(dkgreen) mcolor(dkgreen) msymbol(square_hollow) yaxis(1)) ///
	(connected med year, lcolor(cranberry) mcolor(cranberry) msymbol(diamond_hollow) yaxis(2)) ///	
	if year>=1990 & year<=2019, ///
	xtitle("Calendar Year") ytitle("Benefit Amounts (in 2019 Billion Dollars)", height(8) axis(1)) ///
	ytitle("Medicaid Benefit Amounts (in 2019 Billion Dollars)", height(5) axis(2)) ///
	legend(order(1 "DI" 2 "SNAP" 3 "SSI" 4 "WC" 5 "UI" 6 "TANF" 7 "WIC" 8 "Medicaid") ///
	pos(6) row(1) size(small)) ///
	graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)	
graph export "Figure A1.png", replace	

*************************
******Figures A2-A4******
*************************
use "reg_estimates", clear

**** Set globals for titles and file names 
global snap_othprog_file "Figure A2"
global med_othprog_file "Figure A3"	
global ui_othprog_file "Figure A4"

**** Receipt of other programs 
foreach prog in snap med ui {
	preserve
	
	** Remove existing program
	replace b_`prog'_rec_`prog' = .
	replace b_`prog'_rec_`prog'_nosig = .
	replace b_`prog'_rec_`prog'_sig = .
	
	** Other programs 
	twoway (line b_di_rec_`prog' quarters, lcolor(lavender)) ///
		(scatter b_di_rec_`prog'_sig quarters, ///
		lcolor(lavender) mcolor(lavender) msize(large) msymbol(circle)) ///
		(scatter b_di_rec_`prog'_nosig quarters, ///
		lcolor(lavender) mcolor(lavender) msymbol(smcircle_hollow)) ///
		(line b_wc_rec_`prog' quarters, lcolor(maroon)) ///
		(scatter b_wc_rec_`prog'_sig quarters, ///
		lcolor(maroon) mcolor(maroon) msize(large) msymbol(triangle)) ///
		(scatter b_wc_rec_`prog'_nosig quarters, ///
		lcolor(maroon) mcolor(maroon) msymbol(smtriangle_hollow)) ///		
		(line b_ui_rec_`prog' quarters, lcolor(forest_green)) ///	
		(scatter b_ui_rec_`prog'_sig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msize(large) msymbol(diamond)) ///
		(scatter b_ui_rec_`prog'_nosig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(smdiamond_hollow)) ///	
		(line b_snap_rec_`prog' quarters, lcolor(navy)) ///
		(scatter b_snap_rec_`prog'_sig quarters, ///
		lcolor(navy) mcolor(navy) msize(large) msymbol(circle)) ///
		(scatter b_snap_rec_`prog'_nosig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(smcircle_hollow)) ///
		(line b_tanf_rec_`prog' quarters, lcolor(khaki)) ///
		(scatter b_tanf_rec_`prog'_sig quarters, ///
		lcolor(khaki) mcolor(khaki) msize(large) msymbol(triangle)) ///
		(scatter b_tanf_rec_`prog'_nosig quarters, ///
		lcolor(khaki) mcolor(khaki) msymbol(smtriangle_hollow)) ///		
		(line b_med_rec_`prog' quarters, lcolor(orange_red)) ///
		(scatter b_med_rec_`prog'_sig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msize(large) msymbol(square)) ///
		(scatter b_med_rec_`prog'_nosig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(smsquare_hollow)) ///			
		(line b_ssi_rec_`prog' quarters, lcolor(dkorange)) ///	
		(scatter b_ssi_rec_`prog'_sig quarters, ///
		lcolor(dkorange) mcolor(dkorange) msize(large) msymbol(diamond)) ///
		(scatter b_ssi_rec_`prog'_nosig quarters, ///
		lcolor(dkorange) mcolor(dkorange) msymbol(smdiamond_hollow)) ///	
		(line b_wic_rec_`prog' quarters, lcolor(dkgreen)) ///	
		(scatter b_wic_rec_`prog'_sig quarters, ///
		lcolor(dkgreen) mcolor(dkgreen) msize(large) msymbol(circle)) ///
		(scatter b_wic_rec_`prog'_nosig quarters, ///
		lcolor(dkgreen) mcolor(dkgreen) msymbol(smcircle_hollow)) ///		
		if quarters>=-5 & quarters<=5, ///
		xline(0, lpattern(dash)) yline(0, lcolor(black)) ///	
		xtitle("Quarters Since First Receiving Program") ///
		ytitle("Share of Households Receiving Program" "(Relative to 4 Quarters Pre-Receipt)") ///
		ylabel(, grid gmax gmin) xlabel(-5(1)5) ///
		legend(order(2 "DI (${di_rec_`prog'_mean_round})" 5 "Workers Comp. (${wc_rec_`prog'_mean_round})" ///
		8 "UI (${ui_rec_`prog'_mean_round})" 11 "SNAP (${snap_rec_`prog'_mean_round})" ///
		14 "TANF (${tanf_rec_`prog'_mean_round})" 17 "Medicaid (${med_rec_`prog'_mean_round})" ///
		20 "SSI (${ssi_rec_`prog'_mean_round})" 23 "WIC (${wic_rec_`prog'_mean_round})") ///
		pos(6) row(2) size(small)) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${`prog'_othprog_file}.png", replace			
		
	restore
}

********************
*****Figure A5******
********************
use "reg_estimates", clear

**** Set globals for titles and file names 
global unemployed_any_ytitle "Share of Households with Any Unemployed"
global nilf_25to59_any_ytitle "Share of Households with Any Not in Labor Force"

global unemployed_any_file "Figure A5a"
global nilf_25to59_any_file "Figure A5b"

**** Figures 
foreach var in unemployed_any nilf_25to59_any {
	twoway (rarea ci95ub_`var'_snap ci95lb_`var'_snap quarters, ///
		fi(inten50) lw(none) color(navy%30)) ///
		(line b_`var'_snap quarters, lcolor(navy)) ///
		(scatter b_`var'_snap_sig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(circle)) ///
		(scatter b_`var'_snap_nosig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(circle_hollow)) ///
		(scatter b_`var'_snap_blank quarters, ///
		lcolor(navy) mcolor(navy) msize(large) msymbol(circle)) ///			
		(rarea ci95ub_`var'_med ci95lb_`var'_med quarters, ///
		fi(inten50) lw(none) color(orange_red%30)) ///
		(line b_`var'_med quarters, lcolor(orange_red)) ///
		(scatter b_`var'_med_sig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(triangle)) ///
		(scatter b_`var'_med_nosig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(triangle_hollow)) ///	
		(scatter b_`var'_med_blank quarters, ///
		lcolor(orange_red) mcolor(orange_red) msize(large) msymbol(triangle)) ///			
		(rarea ci95ub_`var'_ui ci95lb_`var'_ui quarters, ///
		fi(inten50) lw(none) color(forest_green%30)) ///
		(line b_`var'_ui quarters, lcolor(forest_green)) ///
		(scatter b_`var'_ui_sig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(square)) ///
		(scatter b_`var'_ui_nosig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(square_hollow)) ///	
		(scatter b_`var'_ui_blank quarters, ///
		lcolor(forest_green) mcolor(forest_green) msize(large) msymbol(square)) ///			
		if quarters>=-5 & quarters<=5, ///
		xline(0, lpattern(solid) lcolor(gs6)) yline(0, lpattern(solid) lcolor(gs6)) ///	
		xtitle("Quarters Since First Receiving Program") ///	
		ytitle("${`var'_ytitle}" "(Relative to 4 Quarters Pre-Receipt)") ///	
		ylabel(, grid gmax gmin) xlabel(-5(1)5) ///		
		legend(order(5 "SNAP (${`var'_snap_mean_round})" 10 "Medicaid (${`var'_med_mean_round})" ///
		15 "UI (${`var'_ui_mean_round})") row(1) pos(6) size(medsmall)) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${`var'_file}.png", replace	
}

********************
*****Figure A6******
********************
use "reg_estimates", clear

**** Scale estimates by pre-receipt mean 
foreach par in b se {
	foreach prog in snap med ui {
		foreach var in thmktinc_pov {
			qui sum `par'_`var'_`prog' if quarters==0
			global `par'_`var'_`prog'_reg = r(mean)/${`var'_`prog'_mean}
			
			qui sum `par'_cohfe_`var'_`prog' if quarters==0
			global `par'_`var'_`prog'_cohfe = r(mean)/${`var'_`prog'_mean}			
			
			qui sum `par'_pois_`var'_`prog' if quarters==0
			global `par'_`var'_`prog'_pois = r(mean)
			
			qui sum `par'_zip_`var'_`prog' if quarters==0
			global `par'_`var'_`prog'_zip = r(mean)			
		
			foreach type in 36mo 9608 pre2 pre4 dou2 dou4 {
				qui sum `par'_`var'_`type'_`prog' if quarters==0
				global `par'_`var'_`prog'_`type' = r(mean)/${`var'_`prog'_mean}	
			}
		}
	}	
}

**** Set up coefficient plots 
foreach prog in snap med ui {
	matrix input mat_`prog'_rchecks = ///
		(${b_thmktinc_pov_`prog'_reg}, ${b_thmktinc_pov_`prog'_cohfe} ///
		${b_thmktinc_pov_`prog'_pois}, ${b_thmktinc_pov_`prog'_zip} ///
		${b_thmktinc_pov_`prog'_36mo}, ${b_thmktinc_pov_`prog'_pre2}, ///
		${b_thmktinc_pov_`prog'_dou2}, ${b_thmktinc_pov_`prog'_pre4}, ///
		${b_thmktinc_pov_`prog'_dou4}, ${b_thmktinc_pov_`prog'_9608} \ ///
		${se_thmktinc_pov_`prog'_reg}, ${se_thmktinc_pov_`prog'_cohfe} ///
		${se_thmktinc_pov_`prog'_pois}, ${se_thmktinc_pov_`prog'_zip} ///
		${se_thmktinc_pov_`prog'_36mo}, ${se_thmktinc_pov_`prog'_pre2}, ///
		${se_thmktinc_pov_`prog'_dou2}, ${se_thmktinc_pov_`prog'_pre4}, ///
		${se_thmktinc_pov_`prog'_dou4}, ${se_thmktinc_pov_`prog'_9608})		
}

**** Figures (incomes)
coefplot (mat(mat_snap_rchecks), ciopts(lcolor(navy)) mcolor(navy) msymbol(circle)) ///
	(mat(mat_med_rchecks), ciopts(lcolor(orange_red)) mcolor(orange_red) msymbol(triangle)) ///
	(mat(mat_ui_rchecks), ciopts(lcolor(forest_green)) mcolor(forest_green) msymbol(square)), ///
	se(2) xline(0, lcolor(gs8) lpattern(dash)) ///
	xtitle("Percent Change in Outcome (Relative to 4 Qtrs Pre-Receipt)", size(small)) ///
	xlabel(-.4(.1).1) ///		
	coeflabel(c1 = "Original" c2 = "Cohort (in lieu of HH) FEs" c3 = "Poisson Specification" ///
	c4 = "Zero-Inflated Poisson Specification" c5 = "Observe 36 Consecutive Mos." ///
	c6 = "Observe 2 Qtrs Pre" c7 = "Observe 2 Qtrs Pre & Post" c8 = "Observe 4 Qtrs Pre" ///
	c9 = "Observe 4 Qtrs Pre & Post" c10 = "Drop 2014-2018 Panels", labsize(small)) ///
	legend(order(2 "SNAP" 4 "Medicaid" 6 "UI") row(1) pos(6)) ///
	graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
graph export "Figure A6.png", replace		

********************
*****Figure A7******
********************
use "reg_estimates", clear

**** Set up coefficient plots for market income 
foreach prog in snap med ui {
	foreach var in thmktinc {
		matrix input mat_`prog'_sumchars = ///
			(${b_`var'_`prog'_white_q5}, ${b_`var'_`prog'_black_q5}, ///
			${b_`var'_`prog'_hisp_q5}, ${b_`var'_`prog'_lesshs_q5}, ///			
			${b_`var'_`prog'_hsgrad_q5}, ${b_`var'_`prog'_college_q5}, ///
			${b_`var'_`prog'_eld_q5}, ${b_`var'_`prog'_wchild_q5}, ///
			${b_`var'_`prog'_nchild_q5} \ ///
			${se_`var'_`prog'_white_q5}, ${se_`var'_`prog'_black_q5}, ///
			${se_`var'_`prog'_hisp_q5}, ${se_`var'_`prog'_lesshs_q5}, ///			
			${se_`var'_`prog'_hsgrad_q5}, ${se_`var'_`prog'_college_q5}, ///
			${se_`var'_`prog'_eld_q5}, ${se_`var'_`prog'_wchild_q5}, ///
			${se_`var'_`prog'_nchild_q5})
	}
}

**** Set globals for file names 
global snap_subgroups_file "Figure A7a"
global med_subgroups_file "Figure A7b"
global ui_subgroups_file "Figure A7c"
			
**** Figures 	
global snap_color navy
global snap_symbol circle
global med_color orange_red
global med_symbol triangle
global ui_color forest_green
global ui_symbol square

foreach prog in snap med ui {
	coefplot (mat(mat_`prog'_sumchars), ///
		ciopts(lcolor(${`prog'_color})) mcolor(${`prog'_color}) msymbol(${`prog'_symbol})), ///
		se(2) xline(0, lcolor(gs8) lpattern(dash)) ///
		xtitle("Percent Change in Market Income (Relative to 4 Qtrs Pre-Receipt)") ///
		coeflabel(c1 = "White Non-Hispanic" c2 = "Black Non-Hispanic" c3 = "Hispanic" ///
		c4 = "Less than High School" c5 = "High School Diploma" c6 = "Some College or More" ///
		c7 = "Elderly HH" c8 = "Non-Eld HH w/ Children" c9 = "Non-Eld HH w/o Children") ///
		legend(off) xlabel(-.4(.1).1) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${dir_results}/${`prog'_subgroups_file}.png", replace			
}

********************
******Table A1******
********************
foreach prog in di wc ui snap tanf med ssi wic {
	use "sipp_sample_analysis", clear		
	
	preserve
	
	**** Keep relevant sample 
	keep if months_since`prog'_min<0 & months_since`prog'_min!=.
	keep if (months_since`prog'>=-15 & months_since`prog'<=16)
	drop if whfnwgt==.
		
	**** Flag unique households 	
	egen households_`prog' = group(spanel ssuid shhadid)
	bys households_`prog': gen dup = cond(_N==1,0,_n)
	replace households_`prog' = . if dup>1
		
	**** Count households by SIPP panel 	
	collapse (count) households_`prog', by(spanel) fast
	gen program = "`prog'"
	save "${temp}/`prog'_samplesize", replace
		
	restore			
}

**** Append across programs 
use "${temp}/snap_samplesize", clear
gen households_`var' = households_snap
drop households_snap

foreach prog in di wc ui tanf med ssi wic {
	append using "${temp}/`prog'_samplesize"
	replace households_`var' = households_`prog' if ///
		households_`var'==.
	drop households_`prog'
}
	
*********************
******Figure B1******
*********************
use "reg_estimates", clear

**** Set globals for titles and file names 
global thmktinc_pov_title_vet "(a) Market Income"
global thallinc_pov_title_vet "(b) Post-Transfer Income"
global notworking_any_title_vet "(c) Non-Employment"
global disabled_any_title_vet "(d) Work-Limiting Disability"
global kid_0to4_any_title_vet "(e) Has Child Aged 0-4"
global sepdivwid_any_title_vet "(f) Separation"

global thmktinc_pov_fileb1 "Figure B1a"
global thallinc_pov_fileb1 "Figure B1b"
global notworking_any_fileb1 "Figure B1c"
global disabled_any_fileb1 "Figure B1d"
global kid_0to4_any_fileb1 "Figure B1e"
global sepdivwid_any_fileb1 "Figure B1f"

foreach var in thmktinc_pov thallinc_pov disabled_any kid_0to4_any ///
sepdivwid_any notworking_any {
	twoway (rarea ci95ub_`var'_vet ci95lb_`var'_vet quarters, ///
		fi(inten50) lw(none) color(navy%30)) ///
		(connected b_`var'_vet quarters, lcolor(navy) mcolor(navy)) ///		
		if quarters>=-5 & quarters<=5, ///
		xline(0, lpattern(solid) lcolor(gs6)) yline(0, lpattern(solid) lcolor(gs6)) ///	
		xtitle("Quarters Since First Receiving Program") ///	
		ytitle("${`var'_ytitle}" "(Relative to 4 Quarters Pre-Receipt)") ///
		ylabel(,grid) xlabel(-5(1)5) title("{bf:${`var'_title_vet}}" " ", size(medsmall)) ///		
		legend(off) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${dir_results}/${`var'_file}.png", replace	
}


